/*
* Copyright (c) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: Camerality info container.
*
*/

// INCLUDE FILES
#include <eikenv.h>
#include <AknIconUtils.h>
#include <avkon.mbg>
#include "cameralitytraces.h"
#include "cameralityglprofiler.h"
#include "cameralityinfocontainer.h"

// ================= MEMBER FUNCTIONS =======================

/*!
* Two-phased constructor.
* @return Container instance.
*/
CCameralityInfoContainer* CCameralityInfoContainer::NewL()
    {
    CCameralityInfoContainer* self = new (ELeave) CCameralityInfoContainer();
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }

/*!
* Constructor.
*/
CCameralityInfoContainer::CCameralityInfoContainer()
    {
    }

/*!
* Second phase constructor.
*/
void CCameralityInfoContainer::ConstructL()
    {
    CCameralityBaseContainer::DoConstructL();
    
    iProfiler = CCameralityGLProfiler::NewL();    
    iPeriodic = CPeriodic::NewL(CActive::EPriorityIdle);
    
    // Create "info" icon
    AknIconUtils::CreateIconL(
            iBitmap, iMask,
            AknIconUtils::AvkonIconFileName(),
            EMbmAvkonQgn_note_info, EMbmAvkonQgn_note_info_mask);
    AknIconUtils::SetSize(iBitmap, TSize(100, 100));
    }

/*!
* Destructor.
* Releases any used resources.
*/
CCameralityInfoContainer::~CCameralityInfoContainer()
    {
    TRACE_ENTER();
    delete iPeriodic;
    delete iProfiler;
    delete iBitmap;
    delete iMask;
    TRACE_EXIT();
    }

/*!
* Normal CCoeControl drawing method.
*/
void CCameralityInfoContainer::Draw(const TRect& /*aRect*/) const
    {
    TRACE_ENTER();

    // Draw transparent background
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
    gc.SetBrushColor(TRgb::Color16MA(0));
    gc.DrawRect(Rect());

    // Draw some info icon
    TSize size(iBitmap->SizeInPixels());
    TPoint p1(Rect().Width()/4 - size.iWidth,
              Rect().Height()/4 - size.iHeight/2);

    gc.BitBltMasked(p1, iBitmap, size, iMask, false);

    // Draw amount of free GPU memory
    if (iProfiler)
        {
        TBuf<255> buffer;
        _LIT(KInfoTxt, "Free GPU memory: %d");
        buffer.Format(KInfoTxt, iProfiler->FreeMemory());

        const CFont* font = CEikonEnv::Static()->NormalFont();
        TInt textWidth = font->TextWidthInPixels(buffer);
        TInt baselineOffset = font->BaselineOffsetInPixels();
        p1.iY += baselineOffset;

        gc.SetBrushStyle(CGraphicsContext::ENullBrush);
        gc.SetPenStyle(CGraphicsContext::ESolidPen);
        gc.SetPenColor(KRgbWhite);
        gc.UseFont(font);
        gc.DrawText(buffer, TPoint(Rect().Width()/4, Rect().Height()/4));
        }

    TRACE_EXIT();
    }

/*!
* Show this control.
*/
void CCameralityInfoContainer::Show(const TRect& extent)
    {
    TRACE_ENTER();
    CCameralityBaseContainer::Show(extent);
    if (iProfiler)
        {
        iProfiler->Start(0);
        }

    if (iPeriodic && !iPeriodic->IsActive())
        {
        iPeriodic->Start(1*1000*1000, 1*1000*1000,
                         TCallBack(RedrawCallback, this));
        }

    TRACE_EXIT();
    }

/*!
* Hide this control.
*/
void CCameralityInfoContainer::Hide()
    {
    if (iPeriodic)
        {
        iPeriodic->Cancel();
        }
    if (iProfiler)
        {
        iProfiler->Stop();
        }

    CCameralityBaseContainer::Hide();
    }

/*!
* Callback for periodically updating the free memory counter.
*/
TInt CCameralityInfoContainer::RedrawCallback(TAny* aInstance)
    {
    CCameralityInfoContainer* instance = (CCameralityInfoContainer*) aInstance;
    instance->DrawDeferred();
    return 0;
    }


// End of file
